package com.company.linkedlist;

public class DoubleLinkedlistDemo {
    public static void main(String[] args) {
        DoubleLinkedlist dll = new DoubleLinkedlist();
        HeroNode2 heroNode0 = new HeroNode2(1001,"盖伦","德玛西亚之力");
        HeroNode2 heroNode1 = new HeroNode2(1004,"拉克丝","光辉女神");
        HeroNode2 heroNode2 = new HeroNode2(1007,"伊泽瑞尔","探险家");
        HeroNode2 heroNode3 = new HeroNode2(1002,"卡莎","虚空之女");
        HeroNode2 heroNode4 = new HeroNode2(1005,"崔斯塔娜","麦林炮手");
        dll.add(heroNode0);
        dll.add(heroNode1);
        dll.add(heroNode2);
        dll.addByOrder(heroNode3);
        dll.showAll();
        System.out.println("==========================================");
        dll.addByOrder(heroNode4);
        dll.showAll();
    }
}


class DoubleLinkedlist{
    private HeroNode2 head=new HeroNode2(0,"","");

    public HeroNode2 getHead(){
        return head;
    }

    public void showAll(){
        if (head.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp=head.next;
        while (temp!=null){
            System.out.println("英雄编号："+temp.no+"\t英雄姓名："+temp.name+"\t英雄昵称："+temp.nickname);
            temp=temp.next;
        }
    }

    public void add(HeroNode2 heroNode){
        HeroNode2 temp=head;
        while (temp.next!=null){
            temp=temp.next;
        }
        temp.next=heroNode;
        heroNode.pre=temp;
        heroNode.next=null;
    }

    public void addByOrder(HeroNode2 heroNode){
        HeroNode2 temp = head.next;
        int heroNo=heroNode.no;
        while (temp!=null){
            if (temp.no>heroNo){
                heroNode.next=temp;
                temp.pre.next=heroNode;
                break;
            }else if(temp.no==heroNo){
                System.out.println("准备插入的英雄编号"+heroNo+"已经存在，不能加入");
                break;
            }
            temp=temp.next;
        }
    }

    public void update(HeroNode2 newHeroNode){
        int heroNo=newHeroNode.no;
        HeroNode2 temp=head.next;
        if (temp==null){
            System.out.println("链表为空~");
            return;
        }
        while (temp!=null){
            if (temp.no==heroNo){
                temp.name=newHeroNode.name;
                temp.nickname=newHeroNode.nickname;
                System.out.println("修改成功！");
                break;
            }
            temp=temp.next;
        }
        if (temp==null){
            System.out.println("没有找到该编号");
        }
    }

    public void delete(int heroNo){
        HeroNode2 temp = head.next;
        if (temp==null){
            System.out.println("该链表为空~");
            return;
        }
        while (temp!=null){
            if (temp.no==heroNo){
                temp.pre.next=temp.next;
                if (temp.next!=null){
                    temp.next.pre=temp.pre;
                }
                System.out.println("删除成功");
                break;
            }
            temp=temp.next;
        }
        if (temp.next==null){
            System.out.println("没有找到该英雄~");
        }
    }

}


class HeroNode2 {
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;
    public HeroNode2 pre;

    public HeroNode2() {
    }

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname +
                '}';
    }
}